package org.vector.serviceImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.vector.bean.User;
import org.vector.service.BookService;
import org.vector.utils.BaseDao;

public class BookServiceImpl implements BookService {
	BaseDao bd = new BaseDao();
	public User Login(User user) {
		String name = user.getName();
    	String password = user.getPassword();
    	String sql = "select name,password from user where name = ? and password = ?";
    	ResultSet resultset = bd.executeQuery(sql, name,password);
    	try {
			if(resultset.next()) {
				return user;
			}else {
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public int Change(String asd,String q ,String w,String e,String s,String t,String y,String a,String d) {
		String sql = "update reader set readerid = ?,readertypeid = ?,readerage = ?"
				+ ",readeradress = ?,readername = ?,readersex = ?,readerphone = ?"
				+ ",readerstate = ? where readerid = ?";
		int r = bd.executeUpdate(sql, q,w,e,s,t,y,a,d,asd);
		return r;
	}
	@Override
	public int Booklook(String asd, String q, String w, String e, String s, String t, String y, String a, String d) {
		String sql = "update book set bookISBN = ?,booktype = ?,bookname = ?"
				+ ",bookauthor = ?,bookpress = ?,bookdate = ?,bookprintnumber = ?"
				+ ",bookprice = ? where bookISBN = ?";
		int r = bd.executeUpdate(sql, q,w,e,s,t,y,a,d,asd);
		return r;
	}
	@Override
	public int BookAdd(String q, String w, String e, String s, String t, String y, String aa, String dd) {
		String sql = "insert into book values(?,?,?,?,?,?,?,?)";
		int row = bd.executeUpdate(sql, q,w,e,s,t,y,aa,dd);
		return row;
	}
	@Override
	public int ChangePassword(User user) {
		String name = user.getName();
		String password = user.getPassword();
		String sql = "update user set password = ? where name = ?";
		int row = bd.executeUpdate(sql, password,name);
		return row;
	}
	@Override
	public int UserAdd(User user) {
		// TODO Auto-generated method stub
		String name = user.getName();
		String password = user.getPassword();
		String sql = "insert into user(name,password) values(?,?)";
		int row = bd.executeUpdate(sql, name,password);
		return row;
	}
	@Override
	public int DeleteUser(String name) {
		String sql = "delete from user where name = ?";
		int row = bd.executeUpdate(sql, name);
		return row;
	}
	@Override
	public int BookTypeAdd(String booktype, String bookname) {
		String sql = "insert into booktype values(?,?)";
		int row = bd.executeUpdate(sql, booktype,bookname);
		return row;
	}
	@Override
	public int ChangeBookType(String asd,String booktype,String bookname) {
		String sql = "update booktype set booktype = ?,bookname = ? where bookname = ?";
		int row = bd.executeUpdate(sql, booktype,bookname,asd);
		return row;
	}
	@Override
	public int DeleteBookType(String asd) {
		String sql = "delete from booktype where bookname = ?";
		int row = bd.executeUpdate(sql, asd);
		return row;
	}
	
	@Override
	public int ReaderTypeAdd(String readerid, String readertype, String maxbook, String loneday) {
		String sql = "insert into readertype values(?,?,?,?)";
		int row = bd.executeUpdate(sql, readerid,readertype,maxbook,loneday);
		return row;
	}
	@Override
	public int ChangeReaderType(String asd, String readerid, String readertype, String maxbook, String loneday) {
		String sql = "update readertype set readerid = ?,readertype = ?,maxbook = ?,loneday = ?  where readerid = ?";
		int row = bd.executeUpdate(sql, readerid,readertype,maxbook,loneday,asd);
		return row;
	}
	@Override
	public int DeleteReaderType(String asd) {
		String sql = "delete from readertype where readerid = ?";
		int row = bd.executeUpdate(sql, asd);
		return row;
	}
	@Override
	public int BookBorrorAdd(String a, String d, String f, String q, String w, String s) {
		String sql = "insert into bookborror values(?,?,?,?,?,?)";
		int row = bd.executeUpdate(sql, a,d,f,q,w,s);
		return row;
	}
	@Override
	public int BookBorrorCome(String readerid) {
		String sql = "delete from bookborror where readerid = ?";
		int row = bd.executeUpdate(sql, readerid);
		return row;
	}
	
}
